package com.w3c.pragmatic.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * 标准响应承接对象.
 *
 * @param <T> 包装的对象
 */
@Data
@AllArgsConstructor
@Builder
public class Response<T> implements Serializable {

  /** 成功 */
  public static final String SUCCESS_CODE = "200";

  /** 失败 */
  public static final String FAIL_CODE = "300";

  /** 响应码 */
  private String code;

  /** 响应信息 */
  private String msg;

  /** 业务数据 */
  private T data;

  public Response() {}

  public static <T> Response<T> ok() {
    return restResult(null, SUCCESS_CODE, null);
  }

  public static <T> Response<T> ok(T data) {
    return restResult(data, SUCCESS_CODE, null);
  }

  public static <T> Response<T> ok(T data, String i, String msg) {
    return restResult(data, SUCCESS_CODE, null);
  }

  public static <T> Response<T> ok(T data, String msg) {
    return restResult(data, SUCCESS_CODE, msg);
  }

  public static <T> Response<T> result(T data, String code, String msg) {
    return restResult(data, code, msg);
  }

  public static <T> Response<T> failed() {
    return restResult(null, FAIL_CODE, null);
  }

  public static <T> Response<T> failed(String msg) {
    return restResult(null, FAIL_CODE, msg);
  }

  public static <T> Response<T> failed(String code, String msg) {
    return restResult(null, code, msg);
  }

  public static <T> Response<T> failed(T data) {
    return restResult(data, FAIL_CODE, null);
  }

  public static <T> Response<T> failed(T data, String msg) {
    return restResult(data, FAIL_CODE, msg);
  }

  public static <T> Response<T> failed(T data, String code, String msg) {
    return restResult(data, code, msg);
  }

  public static <T> Response<T> restResult(T data, String code, String msg) {
    Response<T> apiResult = new Response<>();
    apiResult.setCode(code);
    apiResult.setData(data);
    apiResult.setMsg(msg);
    return apiResult;
  }

  public Boolean isSuccess() {
    return (this.code.equals(SUCCESS_CODE));
  }
}
